package com.nightcolor.im.framework;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nightcolor.im.NightColorApplication;
import com.nightcolor.im.R;

public abstract class BaseActivity extends Activity implements MenuSlideChangeListener {

	private static final String TAG = BaseActivity.class.getName();
	
	protected BaseLayout mRootView;
	
	private TextView mMenuBtn;
	
	protected Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBaseContentView();
		
		mMenuBtn.setOnClickListener(
				new ClickListenerForScrolling(mRootView.mMainView));
		
	}

	private void setBaseContentView() {
		mRootView = new BaseLayout(getApplicationContext());
		
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
		mRootView.mMainView.setMenuWidth(
				NightColorApplication.screenWidth - getResources().
				getDimensionPixelSize(R.dimen.baselayout_bottom_button_width));
		mRootView.mMainView.setCanDragMove(false);
		
		mRootView.mLeftMenu.removeAllViews();
		mRootView.mLeftMenu.addView(genLeftMenuView());
		
		mRootView.mTitleBar.removeAllViews();
		mRootView.mTitleBar.addView(genTitleBar(), lp);
		
		mRootView.mContentView.removeAllViews();
		mRootView.mContentView.addView(genContentView(), lp);
		
		mRootView.mBottomBar.removeAllViews();
		mRootView.mBottomBar.addView(genBottomBar(), lp);
		
		setContentView(mRootView);
		
		mMenuBtn = (TextView) findViewById(R.id.bottombar_menu_btn);
		
	}
	
	protected View genLeftMenuView () {
//		return Utils.inflateLayout(
//				this, R.layout.framework_sidemenu, null);
		return Utils.inflateLayout(
				this, R.layout.default_login_sidemenu, null);
	}
	
	protected View genTitleBar() {
		return Utils.inflateLayout(
				this, R.layout.framework_titlebar, null);
	}
	
	protected View genContentView() {
		return Utils.inflateLayout(
				this, R.layout.framework_content, null);
	}
	
	protected View genBottomBar() {
		return Utils.inflateLayout(
				this, R.layout.framework_bottombar, null);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	
	private class ClickListenerForScrolling implements View.OnClickListener {
        private ScrolledMainView mView;

        public ClickListenerForScrolling(ScrolledMainView scrollView) {
            super();
            this.mView = scrollView;
        }

        @Override
        public void onClick(View v) {
        	if (mView.isMenuVisible()) {
        		mView.hideMenu();
        	}
        	else {
        		mView.showMenu();
        	}
        }
    }


	@Override
	public void onSlideChange(boolean isMenuOpen) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode 
				&& mRootView.mMainView.isMenuVisible()) {
			mRootView.mMainView.hideMenu();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
}
